package component.fulltextsearch.index.handlingtypes;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

import component.fulltextsearch.util.SearchConfigUtil;

/**
 * A FileHandler implementation that delegates responsibility to appropriate
 * DocumentHandler implementation, based on a file extension.
 */
public class ExtensionFileHandler implements IFileHandler {
	private Properties handlerProps;

	public ExtensionFileHandler(){
		handlerProps = SearchConfigUtil.getInstance().getHandlingProperties();
	}

	public ExtensionFileHandler(Properties props) {
		handlerProps = props;
	}

	public String getText(File file) throws FileHandlerException {
		String name = file.getName();
		int dotIndex = name.lastIndexOf(".");
		if ((dotIndex > 0) && (dotIndex < name.length())) {
			String ext = name.substring(dotIndex + 1, name.length());
			String handlerClassName = handlerProps.getProperty(ext);

			if (handlerClassName == null)
				return null;

			try {
				Class handlerClass = Class.forName(handlerClassName);
				IInputStreamHandler handler = (IInputStreamHandler) handlerClass
						.newInstance();
				return handler.getText(new FileInputStream(file));
			} catch (ClassNotFoundException e) {
				throw new FileHandlerException("Cannot create instance of : "
						+ handlerClassName, e);
			} catch (InstantiationException e) {
				throw new FileHandlerException("Cannot create instance of : "
						+ handlerClassName, e);
			} catch (IllegalAccessException e) {
				throw new FileHandlerException("Cannot create instance of : "
						+ handlerClassName, e);
			} catch (FileNotFoundException e) {
				throw new FileHandlerException("File not found: "
						+ file.getAbsolutePath(), e);
			} catch (InputStreamHandlerException e) {
				throw new FileHandlerException("File cannot be handler: "
						+ file.getAbsolutePath(), e);
			}
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		ExtensionFileHandler fileHandler = new ExtensionFileHandler();
		String doc = fileHandler
				.getText(new File(
						"C:\\Documents and Settings\\Administrator\\桌面\\04.GT_QMD_GL_CM_EDM_版本管理指南.doc"));
		System.out.println(doc.length());
		String pptdoc = fileHandler
				.getText(new File(
						"C:\\Documents and Settings\\Administrator\\桌面\\IFPUG CPM4.2.ppt"));
		System.out.println(pptdoc);
		String excel = fileHandler
				.getText(new File(
						"C:\\Documents and Settings\\Administrator\\桌面\\02.GT_GDP_GL_OPD_PDP_项目定义过程.xls"));
		System.out.println(excel);
		String rtf = fileHandler.getText(new File(
				"C:\\Documents and Settings\\Administrator\\桌面\\文档.rtf"));
		System.out.println(rtf);
		String txt = fileHandler
				.getText(new File(
						"C:\\Documents and Settings\\Administrator\\桌面\\Word快捷键列表.txt"));
		System.out.println(txt);

		String pdf = fileHandler
				.getText(new File(
						"C:\\Documents and Settings\\Administrator\\桌面\\Beautiful_Code_Minibook_by_InfoQ.pdf"));
		System.out.println(pdf);
	}
}
